{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']\n",
      " ['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']\n",
      " ['1986' 'Americas' 'Uruguay' 'Other' '0.5']\n",
      " ..., \n",
      " ['1987' 'Africa' 'Malawi' 'Other' '0.75']\n",
      " ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']\n",
      " ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]\n",
      "Help on function genfromtxt in module numpy.lib.npyio:\n",
      "\n",
      "genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)\n",
      "    Load data from a text file, with missing values handled as specified.\n",
      "    \n",
      "    Each line past the first `skip_header` lines is split at the `delimiter`\n",
      "    character, and characters following the `comments` character are discarded.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    fname : file, str, list of str, generator\n",
      "        File, filename, list, or generator to read.  If the filename\n",
      "        extension is `.gz` or `.bz2`, the file is first decompressed. Mote\n",
      "        that generators must return byte strings in Python 3k.  The strings\n",
      "        in a list or produced by a generator are treated as lines.\n",
      "    dtype : dtype, optional\n",
      "        Data type of the resulting array.\n",
      "        If None, the dtypes will be determined by the contents of each\n",
      "        column, individually.\n",
      "    comments : str, optional\n",
      "        The character used to indicate the start of a comment.\n",
      "        All the characters occurring on a line after a comment are discarded\n",
      "    delimiter : str, int, or sequence, optional\n",
      "        The string used to separate values.  By default, any consecutive\n",
      "        whitespaces act as delimiter.  An integer or sequence of integers\n",
      "        can also be provided as width(s) of each field.\n",
      "    skiprows : int, optional\n",
      "        `skiprows` was removed in numpy 1.10. Please use `skip_header` instead.\n",
      "    skip_header : int, optional\n",
      "        The number of lines to skip at the beginning of the file.\n",
      "    skip_footer : int, optional\n",
      "        The number of lines to skip at the end of the file.\n",
      "    converters : variable, optional\n",
      "        The set of functions that convert the data of a column to a value.\n",
      "        The converters can also be used to provide a default value\n",
      "        for missing data: ``converters = {3: lambda s: float(s or 0)}``.\n",
      "    missing : variable, optional\n",
      "        `missing` was removed in numpy 1.10. Please use `missing_values`\n",
      "        instead.\n",
      "    missing_values : variable, optional\n",
      "        The set of strings corresponding to missing data.\n",
      "    filling_values : variable, optional\n",
      "        The set of values to be used as default when the data are missing.\n",
      "    usecols : sequence, optional\n",
      "        Which columns to read, with 0 being the first.  For example,\n",
      "        ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns.\n",
      "    names : {None, True, str, sequence}, optional\n",
      "        If `names` is True, the field names are read from the first valid line\n",
      "        after the first `skip_header` lines.\n",
      "        If `names` is a sequence or a single-string of comma-separated names,\n",
      "        the names will be used to define the field names in a structured dtype.\n",
      "        If `names` is None, the names of the dtype fields will be used, if any.\n",
      "    excludelist : sequence, optional\n",
      "        A list of names to exclude. This list is appended to the default list\n",
      "        ['return','file','print']. Excluded names are appended an underscore:\n",
      "        for example, `file` would become `file_`.\n",
      "    deletechars : str, optional\n",
      "        A string combining invalid characters that must be deleted from the\n",
      "        names.\n",
      "    defaultfmt : str, optional\n",
      "        A format used to define default field names, such as \"f%i\" or \"f_%02i\".\n",
      "    autostrip : bool, optional\n",
      "        Whether to automatically strip white spaces from the variables.\n",
      "    replace_space : char, optional\n",
      "        Character(s) used in replacement of white spaces in the variables\n",
      "        names. By default, use a '_'.\n",
      "    case_sensitive : {True, False, 'upper', 'lower'}, optional\n",
      "        If True, field names are case sensitive.\n",
      "        If False or 'upper', field names are converted to upper case.\n",
      "        If 'lower', field names are converted to lower case.\n",
      "    unpack : bool, optional\n",
      "        If True, the returned array is transposed, so that arguments may be\n",
      "        unpacked using ``x, y, z = loadtxt(...)``\n",
      "    usemask : bool, optional\n",
      "        If True, return a masked array.\n",
      "        If False, return a regular array.\n",
      "    loose : bool, optional\n",
      "        If True, do not raise errors for invalid values.\n",
      "    invalid_raise : bool, optional\n",
      "        If True, an exception is raised if an inconsistency is detected in the\n",
      "        number of columns.\n",
      "        If False, a warning is emitted and the offending lines are skipped.\n",
      "    max_rows : int,  optional\n",
      "        The maximum number of rows to read. Must not be used with skip_footer\n",
      "        at the same time.  If given, the value must be at least 1. Default is\n",
      "        to read the entire file.\n",
      "    \n",
      "        .. versionadded:: 1.10.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray\n",
      "        Data read from the text file. If `usemask` is True, this is a\n",
      "        masked array.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    numpy.loadtxt : equivalent function when no data is missing.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    * When spaces are used as delimiters, or when no delimiter has been given\n",
      "      as input, there should not be any missing data between two fields.\n",
      "    * When the variables are named (either by a flexible dtype or with `names`,\n",
      "      there must not be any header in the file (else a ValueError\n",
      "      exception is raised).\n",
      "    * Individual values are not stripped of spaces by default.\n",
      "      When using a custom converter, make sure the function does remove spaces.\n",
      "    \n",
      "    References\n",
      "    ----------\n",
      "    .. [1] Numpy User Guide, section `I/O with Numpy\n",
      "           <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_.\n",
      "    \n",
      "    Examples\n",
      "    ---------\n",
      "    >>> from io import StringIO\n",
      "    >>> import numpy as np\n",
      "    \n",
      "    Comma delimited file with mixed dtype\n",
      "    \n",
      "    >>> s = StringIO(\"1,1.3,abcde\")\n",
      "    >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),\n",
      "    ... ('mystring','S5')], delimiter=\",\")\n",
      "    >>> data\n",
      "    array((1, 1.3, 'abcde'),\n",
      "          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])\n",
      "    \n",
      "    Using dtype = None\n",
      "    \n",
      "    >>> s.seek(0) # needed for StringIO example only\n",
      "    >>> data = np.genfromtxt(s, dtype=None,\n",
      "    ... names = ['myint','myfloat','mystring'], delimiter=\",\")\n",
      "    >>> data\n",
      "    array((1, 1.3, 'abcde'),\n",
      "          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])\n",
      "    \n",
      "    Specifying dtype and names\n",
      "    \n",
      "    >>> s.seek(0)\n",
      "    >>> data = np.genfromtxt(s, dtype=\"i8,f8,S5\",\n",
      "    ... names=['myint','myfloat','mystring'], delimiter=\",\")\n",
      "    >>> data\n",
      "    array((1, 1.3, 'abcde'),\n",
      "          dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])\n",
      "    \n",
      "    An example with fixed-width columns\n",
      "    \n",
      "    >>> s = StringIO(\"11.3abcde\")\n",
      "    >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],\n",
      "    ...     delimiter=[1,3,5])\n",
      "    >>> data\n",
      "    array((1, 1.3, 'abcde'),\n",
      "          dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])\n",
      "\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\",dtype=str)\n",
    "print(type(world_alcohol))\n",
    "print (world_alcohol)\n",
    "print (help(numpy.genfromtxt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10 15 20]\n",
      "[[ 5 10 15]\n",
      " [20 25 30]\n",
      " [35 40 45]]\n"
     ]
    }
   ],
   "source": [
    "#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "#When we input a list of lists, we get a matrix as a result:\n",
    "matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])\n",
    "print vector\n",
    "print matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "#We can use the ndarray.shape property to figure out how many elements are in the array\n",
    "vector = numpy.array([1, 2, 3, 4])\n",
    "print(vector.shape)\n",
    "#For matrices, the shape property contains a tuple with 2 elements.\n",
    "matrix = numpy.array([[5, 10, 15], [20, 25, 30]])\n",
    "print(matrix.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy\n",
    "#Each value in a NumPy array has to have the same data type\n",
    "#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. \n",
    "#You can check the data type of a NumPy array using the dtype property.\n",
    "numbers = numpy.array([1, 2, 3, 4])\n",
    "print (numbers)\n",
    "numbers.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[             nan,              nan,              nan,\n",
       "                     nan,              nan],\n",
       "       [  1.98600000e+03,              nan,              nan,\n",
       "                     nan,   0.00000000e+00],\n",
       "       [  1.98600000e+03,              nan,              nan,\n",
       "                     nan,   5.00000000e-01],\n",
       "       ..., \n",
       "       [  1.98700000e+03,              nan,              nan,\n",
       "                     nan,   7.50000000e-01],\n",
       "       [  1.98900000e+03,              nan,              nan,\n",
       "                     nan,   1.50000000e+00],\n",
       "       [  1.98500000e+03,              nan,              nan,\n",
       "                     nan,   3.10000000e-01]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#When NumPy can't convert a value to a numeric data type like float or integer, it uses a special nan value that stands for Not a Number\n",
    "#nan is the missing data\n",
    "#1.98600000e+03 is actually 1.986 * 10 ^ 3\n",
    "world_alcohol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']\n",
      " ['1986' 'Americas' 'Uruguay' 'Other' '0.5']\n",
      " ['1985' 'Africa' \"Cte d'Ivoire\" 'Wine' '1.62']\n",
      " ..., \n",
      " ['1987' 'Africa' 'Malawi' 'Other' '0.75']\n",
      " ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']\n",
      " ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]\n"
     ]
    }
   ],
   "source": [
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\", dtype=str, skip_header=1)\n",
    "print(world_alcohol)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "Cte d'Ivoire\n"
     ]
    }
   ],
   "source": [
    "uruguay_other_1986 = world_alcohol[1,4]\n",
    "third_country = world_alcohol[2,2]\n",
    "print uruguay_other_1986\n",
    "print third_country"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10 15]\n"
     ]
    }
   ],
   "source": [
    "vector = numpy.array([5, 10, 15, 20])\n",
    "print(vector[0:3])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 25 40]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 10]\n",
      " [20 25]\n",
      " [35 40]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[:,0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[20 25]\n",
      " [35 40]]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([\n",
    "                    [5, 10, 15], \n",
    "                    [20, 25, 30],\n",
    "                    [35, 40, 45]\n",
    "                 ])\n",
    "print(matrix[1:3,0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
